import areaList from '../../utils/areaList';
const app = getApp()

Page({

  data: {
    act_list: [],
    sift_obj: {
      area: "",
      province: "",
      city: "",
    },

    key_word: "",
    is_end: false,
    areaList: areaList,
    area: '地区',
    grade: '等级',
    grade_value: 1,
    sift_option_1: [{
        text: '综合',
        value: '1'
      },
      {
        text: '人气',
        value: '2'
      }
    ],
    sift_option_2: [{
        text: '价格升序',
        value: '2'
      },
      {
        text: '价格降序',
        value: '3'
      }
    ],
    hidden: true
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    console.log(res, '88888888888888888888')
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
    } else {
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length - 1] //获取当前页面的对象
      var url = currentPage.route //当前页面url
      console.log(url, '888888888888')
      console.log("来自右上角转发菜单")
      return {
        title: '分享至朋友圈',
        path: url,
        success: (res) => {
          console.log("转发成功", res);
        },
        fail: (res) => {
          console.log("转发失败", res);
        }
      }
    }

  },
  area_change(e) {
    let area = e.detail.values[2].name
    let province = e.detail.values[0].name
    let city = e.detail.values[1].name
    let position = e.detail.values[2].name
    if (province == "全部") {
      province = ""
      area = "全部"
    }
    this.setData({
      area: area,
      'sift_obj.province': province,
      'sift_obj.city': city,
      'sift_obj.area': position
    })

    this.get_act_list()

  },

  is_end_change(e) {
    this.setData({
      is_end: e.detail
    });
  },

  get_act_list() {
    let sift_obj = this.data.sift_obj
    let that = this
    app.request("/get_activity", {
      data: sift_obj
    }).then(res => {
      console.log(res);

      let act_list = res.data.data
      that.setData({
        act_list
      })

      if (res.data.data != '') {
        console.log('9999999999999999999')
        that.setData({
          hidden: that.data.hidden
        });
      } else {
        that.setData({
          hidden: !that.data.hidden
        });
      }

      //隐藏loading 提示框
      wx.hideLoading();
      //隐藏导航条加载动画
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
      console.log(that.data.act_list);

    })
  },

  go_act(e) {
    // console.log(e.currentTarget.dataset.id);
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/activity_info/activity_info?id=' + id,
    })
  },


  go_search() {
    wx.navigateTo({
      url: '/pages/search_act/search_act',
    })
  },

  //校验缓存中的token 和 通过openid拿到的token进行比对
  check_token() {
    var that = this
    var user_info_token = wx.getStorageSync('token');
    //获取缓存中的openid或者是全局当中的openid变量 通过指定openid查询后台的token 
    wx.request({
      url: 'https://vip.join.ink/api/get_token',
      data: {
        openid: wx.getStorageSync('openid'), //获取缓存中的openid openid不会变
        token: user_info_token
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        if (res.data.code == 200) {
          //此时重新设置新的token作为缓存对象
          wx.setStorageSync('token', res.data.api_token)
          wx.setStorageSync('token_is_agreement', res.data.data) //一致情况下为1
        } else {
          //缓存中的token和后台当中的token不一致  需要重新授权登录
          wx.removeStorageSync('token')
          if (user_info_token != '') {
            wx.showModal({
              title: '提示',
              content: 'token失效，请重新登录！',
              showCancel: false
            })
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/personal/personal',
              })
            }, 2000);
          }

        }
      }
    })


  },
  //刷新
  onRefresh() {
    var that = this
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '正在刷新...',
    })
    that.get_act_list()
    that.check_token()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
  },

  onLoad: function (options) {
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log(res)
      },
      fail(e) {
        console.log(e)
      }
    })
  },

  onShow: function () {
    this.get_act_list()
    this.check_token()
  }

})